<?php        
    class TemplateExerciseSetModel extends Model {
        public static $classDesc = "Template exercise set";
        
        public static $keyDesc = array(
            "id"                    => "ID",
            "template_exerciseid"   => "Template exercise ID",
            "exerciseid"            => "Exercise ID",
            "repetitions"           => "Reps",
            "repetitions_end"       => "Reps (end)",
            "weight_kg"             => "Weight (KG)",
            "weight_lb"             => "Weight (LB)",
            "onerepmax_percent"     => "% of 1RM",
            "repetitions_type"      => "Rep type",
            "barbell_type"          => "Barbell type",
            "rest_interval_sec"     => "Rest"
        );
        
        public function __construct($id = null) {
            static $exerciseCategories = array();
            
            $this->_table = "template_exercise_set";            
            parent::__construct($id);
            
            # Aseta maksiarvot
            parent::$_schema["template_exercise_set"]["onerepmax_percent"]["max"] = 100;
            
            # Levytankotyypin validointi
            parent::$_schema["template_exercise_set"]["barbell_type"]["validator"] =
                function($key,$value,$data) use (&$exerciseCategories) {
                    $exerciseid = arrGet($data,"exerciseid");
                    if ($exerciseid && $value) {
                        if (!isset($exerciseCategories[$exerciseid])) {
                            $exerciseCategories[$exerciseid] = (new ExerciseModel($exerciseid))->
                                category;
                        }
                        
                        if ($exerciseCategories[$exerciseid] != "Barbell") {
                            throw new ValidationException(
                                "Only barbell exercises can have barbell type",
                                3000,
                                get_class($this),
                                $key,
                                $value                        
                            );
                        }
                    }
                };
        }
    }